package com.booksystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.booksystem.common.ResultCodeConstant;
import com.booksystem.dto.PromotionCouponDTO;
import com.booksystem.dto.PromotionCouponDeleteDTO;
import com.booksystem.dto.PromotionCouponQueryDTO;
import com.booksystem.dto.PromotionCouponUpdateStatusDTO;
import com.booksystem.entity.PromotionCouponDO;
import com.booksystem.exception.BusinessException;
import com.booksystem.mapper.PromotionCouponMapper;
import com.booksystem.service.PromotionCouponService;
import java.util.Date;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *   促销优惠券服务实现类
 * </p>
 * @author 段伟豪
 */
@Service
public class PromotionCouponServiceImpl extends ServiceImpl<PromotionCouponMapper, PromotionCouponDO> implements PromotionCouponService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addPromotionCoupon(PromotionCouponDTO dto) {
        // 校验优惠券编码是否已存在
        QueryWrapper<PromotionCouponDO> wrapper = new QueryWrapper<>();
        wrapper.eq("coupon_code", dto.getCouponCode());
        PromotionCouponDO existingCoupon = this.getOne(wrapper);
        if (existingCoupon != null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }

        // 构造实体对象并保存
        PromotionCouponDO couponDO = new PromotionCouponDO();
        couponDO.setCouponCode(dto.getCouponCode());
        couponDO.setCouponName(dto.getCouponName());
        couponDO.setDiscountType(dto.getDiscountType());
        couponDO.setDiscountValue(dto.getDiscountValue());
        couponDO.setMinAmount(dto.getMinAmount());
        couponDO.setStartTime(dto.getStartTime());
        couponDO.setEndTime(dto.getEndTime());
        couponDO.setMaxUseTimes(dto.getMaxUseTimes());
        couponDO.setUsedCount(0);
        couponDO.setStatus(dto.getStatus());
        couponDO.setCreateTime(new Date());
        couponDO.setUpdateTime(new Date());

        return this.save(couponDO);
    }

    @Override
    public Page<PromotionCouponDO> listPromotionCoupons(PromotionCouponQueryDTO queryDTO) {
        Page<PromotionCouponDO> page = new Page<>(queryDTO.getCurrentPage(), queryDTO.getPageSize());
        QueryWrapper<PromotionCouponDO> wrapper = new QueryWrapper<>();

        if (queryDTO.getCouponName() != null && !queryDTO.getCouponName().isEmpty()) {
            wrapper.like("coupon_name", queryDTO.getCouponName());
        }
        if (queryDTO.getStatus() != null) {
            wrapper.eq("status", queryDTO.getStatus());
        }

        // 按创建时间倒序排列
        wrapper.orderByDesc("create_time");

        return this.page(page, wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updatePromotionCouponStatus(PromotionCouponUpdateStatusDTO dto) {
        // 判断优惠券是否存在
        PromotionCouponDO couponDO = this.getById(dto.getCouponId());
        if (couponDO == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, "优惠券不存在");
        }

        // 更新状态
        couponDO.setStatus(dto.getStatus());
        couponDO.setUpdateTime(new Date());
        return this.updateById(couponDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deletePromotionCoupon(PromotionCouponDeleteDTO dto) {
        // 判断优惠券是否存在
        PromotionCouponDO couponDO = this.getById(dto.getCouponId());
        if (couponDO == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, "优惠券不存在");
        }

        // 删除记录
        return this.removeById(dto.getCouponId());
    }
}